package com.bread.tliaswebmanagement.service.impl;

import com.bread.tliaswebmanagement.mapper.EmpExprMapper;
import com.bread.tliaswebmanagement.mapper.EmpMapper;
import com.bread.tliaswebmanagement.pojo.*;
import com.bread.tliaswebmanagement.service.EmpLogService;
import com.bread.tliaswebmanagement.service.EmpService;
import com.bread.tliaswebmanagement.utils.CurrentHolder;
import com.bread.tliaswebmanagement.utils.JwtUtils;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
@Slf4j
public class EmpServiceImpl implements EmpService {

    @Autowired
    private EmpMapper empMapper;
    @Autowired
    private EmpExprMapper empExprMapper;
    @Autowired
    private EmpLogService empLogService;

    @Override
    public PageResult<Emp> page(EmpQueryParam empQueryParam) {
        PageHelper.startPage(empQueryParam.getPage(), empQueryParam.getPageSize());
        Page<Emp> resultPage = (Page<Emp>) empMapper.list(empQueryParam);
        return new PageResult<Emp>(resultPage.getTotal(), resultPage.getResult());
    }

    @Transactional(rollbackFor = {Exception.class})
    @Override
    public void add(Emp emp) {
        try {
            LocalDateTime now = LocalDateTime.now();
            emp.setCreateTime(now);
            emp.setUpdateTime(now);
            empMapper.insert(emp);
            List<EmpExpr> exprList = emp.getExprList();
            if (!CollectionUtils.isEmpty(exprList)){
                exprList.forEach(expr -> {
                    expr.setEmpId(emp.getId());
                });
                empExprMapper.insertBatch(exprList);
            }
        } finally {
            EmpLog empLog = new EmpLog(null, LocalDateTime.now(), "新增员工:"+emp);
            empLogService.insertLog(empLog);
        }
    }

    @Transactional(rollbackFor = {Exception.class})
    @Override
    public void deleteByIds(List<Integer> ids) {
        try {
            empMapper.deleteByIds(ids);
            empExprMapper.deleteByEmpIds(ids);
        } finally {
            EmpLog empLog = new EmpLog(null, LocalDateTime.now(), "删除员工:"+ids);
            empLogService.insertLog(empLog);
        }
    }

    @Override
    public Emp getById(Integer id) {
        return empMapper.getById(id);
    }

    @Transactional(rollbackFor = {Exception.class})
    @Override
    public void update(Emp emp) {
        try {
            emp.setUpdateTime(LocalDateTime.now());
            empMapper.update(emp);
            //修改工作经历，统一先删除再添加
            empExprMapper.deleteByEmpIds(List.of(emp.getId()));
            List<EmpExpr> exprList = emp.getExprList();
            if (!CollectionUtils.isEmpty(exprList)){
                exprList.forEach(expr -> {
                    expr.setEmpId(emp.getId());
                });
                empExprMapper.insertBatch(exprList);
            }
        } finally {
            EmpLog empLog = new EmpLog(null, LocalDateTime.now(), "修改员工:"+emp);
            empLogService.insertLog(empLog);
        }
    }

    @Override
    public List<Emp> list() {
        return empMapper.list(null);
    }

    @Override
    public LoginResult login(Emp emp) {
        CurrentHolder.set(emp);
        log.info("将登录请求信息加入ThreadLocal: {},{}",emp.getUsername(),emp.getPassword());
        Emp e = empMapper.selectByUsernameAndPassword(emp);
        if (e != null) {
            //生成jwt令牌
            Map<String, Object> data = new HashMap<>();
            data.put("id", e.getId());
            data.put("username", e.getUsername());
            String jwt = JwtUtils.generateJwt(data);
            return new LoginResult(e.getId(), e.getUsername(), e.getName(), jwt);
        } else {
            return null;
        }

    }

    /*@Override
    public PageResult<Emp> page(Integer page, Integer pageSize) {
        Long total = empMapper.count();
        Integer start = (page-1) * pageSize;
        List<Emp> emps = empMapper.selectByPage(start, pageSize);
        return new PageResult<Emp>(total, emps);
    }*/



}
